home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CGAMEJOY.ARJ / JOYSTKLO.SE < prev    next >
Text File  |  1992-01-08  |  9KB  |  202 lines

  1. joystklo.se      Low Level Joystick Routines based on JOY.ARC
  2.                     Preliminary Structured English
  3.  
  4. MODULES:
  5.     Low Level       /* this module */
  6.  
  7.                 1: detect               : presence through bios or port
  8.                 2: get_valid_axes       : return mask of valid axes
  9.                 3: get_active_axes      : return mask of active axes
  10.                 4: set_active_axes      : set mask of active axes
  11.                 5  button_state         ; current button state
  12.                 6: button_change        : returns bitmask of button changes
  13.                 7: axis_position        : gets position of given axis
  14.  
  15.     Middle Level    /* joystkmd.se */
  16.  
  17.                 8: initialize           : detect & inititalize joystick globals
  18.                 9: axis_position_change : returns changes in active axes
  19.                10: real_coords_to_rel   : translates axis pos relative to center
  20.                11: get_center_coords    : gets center position of joystick
  21.                12: set_center_coords    : grabs current position as new center
  22.                13: get_min_coords       ; gets minimum position of joystick
  23.                14: set_min_coords       ; grabs current position as new minimum
  24.                15: get_max_coords       ; gets maximum position of joystick
  25.                16: set_max_coords       ; grabs current position as new maximum
  26.                17: get_tolerance        : get tolerance of change to ignore
  27.                18: set_tolerance        : set tolerance of change to ignore
  28.  
  29.     High Level    /* do it yo-sef */
  30.  
  31.                19: create               : grab interrupt 1Ch & set up queue
  32.                20: destroy              : release interrupt 1Ch & queue
  33.                21: get_joystick_event   : get event from queue
  34.                22: push_joystick_event  : push event into queue
  35.                23: interrupt_handler    : timer interrupt 1Ch polling routine
  36.  
  37. PURPOSE:
  38.             To create an event compatible set of routines to read input from
  39.             the game port. (particularly geared to joysticks).
  40.             Based on the file c_joy.c written by Gary Blaine,
  41.             CIS 72707,1736  March 4, 1990 which is available in the
  42.             Borland C Programming Forum on Compuserve in the file JOY.ARC.
  43.             Use assembly for time-critical operations.
  44.  
  45. GLOBAL DEFINES:
  46.          ; return values for button_change and button state
  47.  
  48.             JA1_BUTTON_DOWN        16        ; joystick A, button 1, down
  49.             JA1_BUTTON_UP           1        ; joystick A, button 1, up
  50.             JA2_BUTTON_DOWN        32        ; joystick A, button 2, down
  51.             JA2_BUTTON_UP           2        ; joystick A, button 2, up
  52.  
  53.             JB1_BUTTON_DOWN        64        ; joystick B, button 1, down
  54.             JB1_BUTTON_UP           4        ; joystick B, button 1, up
  55.             JB2_BUTTON_DOWN       128        ; joystick B, button 2, down
  56.             JB2_BUTTON_UP           8        ; joystick B, button 2, up
  57.  
  58.             J_ANY_BUTTON_DOWN    0xf0        ; any button down: either stick
  59.  
  60.             J_A_BUTTON_DOWN      0x30        ; any button down: joystick A
  61.             J_B_BUTTON_DOWN      0xC0        ; any button down: joystick B
  62.  
  63.             J_BUTTON_1_DOWN      0x50        ; button 1 down: either joystick
  64.             J_BUTTON_2_DOWN      0xA0        ; button 2 down: either joystick
  65.  
  66.          ; parameters for axis_position and set_active_axes
  67.  
  68.             JAX_AXIS                1        ; x axis mask for joystick A
  69.             JAY_AXIS                2        ; y axis mask for joystick A
  70.             JBX_AXIS                4        ; x axis mask for joystick B
  71.             JBY_AXIS                8        ; y axis mask for joystick B
  72.  
  73.          ; parameters for set_active_axes
  74.  
  75.             JOYSTICK_A              3        ; axis mask for joystick A
  76.             JOYSTICK_B           0x0C        ; axis mask for joystick B
  77.               
  78. GLOBAL VARIABLES:
  79.             G_Stick_Last_Button:    bit image of last button state
  80.  
  81.             G_Stick_Active_Axes:    bit mask of currently active axes
  82.             G_Stick_Valid_Axes:     bit mask of valid axes
  83.  
  84. /*---------------------------------------------------------------------*/
  85. get_valid_axes
  86.     BEGIN
  87.         return  G_Stick_Valid_Axes
  88.     END
  89. /*---------------------------------------------------------------------------*/
  90. get_active_axes
  91.     BEGIN
  92.         return  G_Stick_Active_Axes
  93.     END
  94. /*---------------------------------------------------------------------------*/
  95. set_active_axes( new_mask )
  96.     parameters:     new_mask
  97.     variables:      check_val
  98.     BEGIN
  99.         set check_val to new_mask AND G_Stick_Valid_Axes
  100.         if check_val != new_mask
  101.             return error
  102.         set G_Stick_Active_Axes to new_mask
  103.             return success
  104.     END
  105. /*---------------------------------------------------------------------*/
  106. detect
  107.     globals:G_Stick_Active_Axes, G_Stick_Valid_Axes
  108.     BEGIN
  109.         trigger game adapter one shot timers
  110.         wait for timers to flip back
  111.         read game adapter into game_value1
  112.         trigger game adapter one shot timers
  113.         read game adapter into game_value2
  114.         valid_axes = bits differing between game_value1 & game_value2 (XOR)
  115.         G_Stick_Active_Axes = valid_axes
  116.         G_Stick_Valid_Axes = valid_axes
  117.         return valid_axes
  118.     END
  119.  
  120. /*---------------------------------------------------------------------*/
  121. button_state
  122.     registers : current_state, changed_bit_mask, button_downs, button_ups
  123.                 changed_event_bitfield, last_button
  124.     BEGIN
  125.         read game port
  126.         AND current_state with 0xf0 to lose lower nibble
  127.         save current_state to G_Stick_Last_Button
  128.         XOR current_state with 0xf0 to flip bits
  129.         save current_state temporarily
  130.         XOR current_state with 0xf0 to get button_ups
  131.         SHIFT button_ups right 4 bits into lower nibble
  132.         OR button_downs with button_ups to get button_state
  133.         return button_state
  134.     END button_state
  135.    
  136.  
  137. /*---------------------------------------------------------------------*/
  138. button_change
  139.     globals   : G_Stick_Last_Button
  140.     registers : current_state, changed_bit_mask, button_downs, button_ups
  141.                 changed_event_bitfield, last_button
  142.     BEGIN
  143.         read game port
  144.         loose lower nibble
  145.         save current_state temporarily
  146.         XOR current_state with G_Stick_Last_Button to get changed_bit_mask
  147.         if changed_bit_mask is NULL,
  148.             return NULL
  149.         else
  150.             save G_Stick_Last_Button to last_button
  151.             save current_state to G_Stick_Last_Button
  152.             AND G_Stick_Last_Button with changed_bit_mask to get button_downs
  153.             XOR button_downs with changed_bit_mask to get button_ups
  154.             SHIFT button_ups right 4 bits into lower nibble
  155.             OR button_downs with button_ups to get changed_event_bitfield
  156.             return changed_event_bitfield
  157.     END button_change
  158.    
  159. /*---------------------------------------------------------------------*/
  160. axis_position
  161.     parameters: axis_mask
  162.     registers: counter, game_value, start_time, end_time, result
  163.     defines: time_out
  164.     BEGIN
  165.         get currently active axes from G_Stick_Active_Axes
  166.         if given axis is not active (valid)
  167.             return with error
  168.         disable interrupts
  169.         latch 8253 timer 0
  170.         read start_time from 8253
  171.         trigger game adapter
  172.         do
  173.             read game adapter into game_value
  174.             if game_value AND axis_mask == 0    /* adapter bit has fired */
  175.                 end while loop
  176.             increment the counter
  177.             while counter != time_out
  178.         if counter == time_out
  179.             enable interrupts
  180.             return NULL
  181.         latch 8253 timer 0
  182.         read end_time from 8253
  183.         enable interrupts
  184.  
  185.         if start_time >= end_time
  186.             result = start_time - end_time
  187.         else
  188.             result = FFFFh - end_time + start_time
  189.         result = result AND 1FF0h
  190.         result = result >> 4
  191.  
  192.         do
  193.             read game adapter into game_value
  194.             if game_value AND Fh == 0         /* all adapter bits have fired */
  195.                 end while loop
  196.             increment the counter
  197.             while counter != time_out
  198.         return result
  199.     END axis_position
  200.  
  201. /*-- end joystklo.se ------------------*/
  202.